﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Stock.Dal;
using Stock.Model;

namespace Stock.Bl
{
    /// <summary>
    /// StockInfoBl
    /// </summary>
    public partial class StockInfoBl
    {
        private readonly StockInfoDal dal = new StockInfoDal();

        public StockInfoBl()
        { }

        #region Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(StockInfoEntity model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(StockInfoEntity model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string S_Code)
        {
            return dal.Delete(S_Code);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string S_Codelist)
        {
            return dal.DeleteList(S_Codelist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StockInfoEntity GetModel(string S_Code)
        {
            return dal.GetModel(S_Code);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<StockInfoEntity> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<StockInfoEntity> DataTableToList(DataTable dt)
        {
            List<StockInfoEntity> modelList = new List<StockInfoEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                StockInfoEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new StockInfoEntity();
                    if (dt.Rows[n]["S_Code"] != null && dt.Rows[n]["S_Code"].ToString() != "")
                    {
                        model.S_Code = dt.Rows[n]["S_Code"].ToString();
                    }
                    if (dt.Rows[n]["S_Name"] != null && dt.Rows[n]["S_Name"].ToString() != "")
                    {
                        model.S_Name = dt.Rows[n]["S_Name"].ToString();
                    }
                    if (dt.Rows[n]["S_PYM"] != null && dt.Rows[n]["S_PYM"].ToString() != "")
                    {
                        model.S_PYM = dt.Rows[n]["S_PYM"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        public void UpdateAllInfo()
        {
            string filename = @"c:\newStockInfo.txt";
            //打开文件并显示其内容
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(filename, System.Text.Encoding.Default);

                Stock.common.PinYin newPinYin = new Stock.common.PinYin();
                StockInfoEntity model;
                dal.DeleteAll();
                for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                {
                    model = new StockInfoEntity();
                    if (line.StartsWith("18"))
                    {
                        Console.Write("");
                    }
                    string[] inData = line.Split(new string[] { "\t" }, System.StringSplitOptions.None);
                    model.S_Code = inData[0];
                    model.S_Name = inData[1];
                    model.S_PYM = newPinYin.GetChineseSpell(model.S_Name);

                    dal.Add(model);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }
    }

        #endregion Method
}